package com.example.logaspect.config;

import com.example.logaspect.aspect.AnnotationLogAspect;
import com.example.logaspect.aspect.GlobalLogAspect;
import com.example.logaspect.service.DefaultLogRecordServiceImpl;
import com.example.logaspect.service.LogRecordService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 日志切面自动配置类
 */
@Configuration
@EnableConfigurationProperties(LogAspectProperties.class)
@ComponentScan(basePackages = "com.example.logaspect")
@ConditionalOnProperty(prefix = "logging.aspect", name = "enabled", havingValue = "true", matchIfMissing = true)
public class LogAspectAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public LogRecordService logRecordService() {
        return new DefaultLogRecordServiceImpl();
    }

    @Bean
    @ConditionalOnProperty(prefix = "logging.aspect", name = "enable-annotation-aspect", havingValue = "true", matchIfMissing = true)
    public AnnotationLogAspect annotationLogAspect(LogRecordService logRecordService, LogAspectProperties properties) {
        return new AnnotationLogAspect(logRecordService, properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "logging.aspect", name = "enable-global-aspect", havingValue = "true", matchIfMissing = false)
    public GlobalLogAspect globalLogAspect(LogRecordService logRecordService, LogAspectProperties properties) {
        return new GlobalLogAspect(logRecordService, properties);
    }
}